home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / tiff / libtiff / tif_thunder.c < prev    next >
C/C++ Source or Header  |  1992-02-10  |  5KB  |  155 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/tif_thunder.c,v 1.19 92/02/10 19:06:46 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. /*
  30.  * TIFF Library.
  31.  *
  32.  * ThunderScan 4-bit Compression Algorithm Support
  33.  */
  34. #include "tiffioP.h"
  35.  
  36. /*
  37.  * ThunderScan uses an encoding scheme designed for
  38.  * 4-bit pixel values.  Data is encoded in bytes, with
  39.  * each byte split into a 2-bit code word and a 6-bit
  40.  * data value.  The encoding gives raw data, runs of
  41.  * pixels, or pixel values encoded as a delta from the
  42.  * previous pixel value.  For the latter, either 2-bit
  43.  * or 3-bit delta values are used, with the deltas packed
  44.  * into a single byte.
  45.  */
  46. #define    THUNDER_DATA        0x3f    /* mask for 6-bit data */
  47. #define    THUNDER_CODE        0xc0    /* mask for 2-bit code word */
  48. /* code values */
  49. #define    THUNDER_RUN        0x00    /* run of pixels w/ encoded count */
  50. #define    THUNDER_2BITDELTAS    0x40    /* 3 pixels w/ encoded 2-bit deltas */
  51. #define        DELTA2_SKIP        2    /* skip code for 2-bit deltas */
  52. #define    THUNDER_3BITDELTAS    0x80    /* 2 pixels w/ encoded 3-bit deltas */
  53. #define        DELTA3_SKIP        4    /* skip code for 3-bit deltas */
  54. #define    THUNDER_RAW        0xc0    /* raw data encoded */
  55.  
  56. static const int twobitdeltas[4] = { 0, 1, 0, -1 };
  57. static const int threebitdeltas[8] = { 0, 1, 2, 3, 0, -3, -2, -1 };
  58.  
  59. #define    SETPIXEL(op, v) { \
  60.     lastpixel = (v) & 0xf; \
  61.     if (npixels++ & 1) \
  62.         *op++ |= lastpixel; \
  63.     else \
  64.         op[0] = lastpixel << 4; \
  65. }
  66.  
  67. static int
  68. ThunderDecode(tif, op, maxpixels)
  69.     TIFF *tif;
  70.     register u_char *op;
  71.     int maxpixels;
  72. {
  73.     register u_char *bp;
  74.     register int n, cc, lastpixel, npixels, delta;
  75.  
  76.     bp = (u_char *)tif->tif_rawcp;
  77.     cc = tif->tif_rawcc;
  78.     lastpixel = npixels = 0;
  79.     while (cc > 0 && npixels < maxpixels) {
  80.         n = *bp++, cc--;
  81.         switch (n & THUNDER_CODE) {
  82.         case THUNDER_RUN:        /* pixel run */
  83.             /*
  84.              * Replicate the last pixel n times,
  85.              * where n is the lower-order 6 bits.
  86.              */
  87.             if (npixels & 1) {
  88.                 op[0] |= lastpixel;
  89.                 lastpixel = *op++; npixels++; n--;
  90.             } else
  91.                 lastpixel |= lastpixel << 4;
  92.             npixels += n;
  93.             for (; n > 0; n -= 2)
  94.                 *op++ = lastpixel;
  95.             if (n == -1)
  96.                 *--op &= 0xf0;
  97.             lastpixel &= 0xf;
  98.             break;
  99.         case THUNDER_2BITDELTAS:    /* 2-bit deltas */
  100.             if ((delta = ((n >> 4) & 3)) != DELTA2_SKIP)
  101.                 SETPIXEL(op, lastpixel + twobitdeltas[delta]);
  102.             if ((delta = ((n >> 2) & 3)) != DELTA2_SKIP)
  103.                 SETPIXEL(op, lastpixel + twobitdeltas[delta]);
  104.             if ((delta = (n & 3)) != DELTA2_SKIP)
  105.                 SETPIXEL(op, lastpixel + twobitdeltas[delta]);
  106.             break;
  107.         case THUNDER_3BITDELTAS:    /* 3-bit deltas */
  108.             if ((delta = ((n >> 3) & 7)) != DELTA3_SKIP)
  109.                 SETPIXEL(op, lastpixel + threebitdeltas[delta]);
  110.             if ((delta = (n & 7)) != DELTA3_SKIP)
  111.                 SETPIXEL(op, lastpixel + threebitdeltas[delta]);
  112.             break;
  113.         case THUNDER_RAW:        /* raw data */
  114.             SETPIXEL(op, n);
  115.             break;
  116.         }
  117.     }
  118.     tif->tif_rawcp = (char *)bp;
  119.     tif->tif_rawcc = cc;
  120.     if (npixels != maxpixels) {
  121.         TIFFError(tif->tif_name,
  122.             "ThunderDecode: %s data at scanline %d (%d != %d)",
  123.             npixels < maxpixels ? "Not enough" : "Too much",
  124.             tif->tif_row, npixels, maxpixels);
  125.         return (0);
  126.     }
  127.     return (1);
  128. }
  129.  
  130. static int
  131. ThunderDecodeRow(tif, buf, occ, s)
  132.     TIFF *tif;
  133.     u_char *buf;
  134.     int occ;
  135.     u_int s;
  136. {
  137.     u_char *row = buf;
  138.     
  139.     while (occ > 0) {
  140.         if (!ThunderDecode(tif, row, tif->tif_dir.td_imagewidth))
  141.             return (0);
  142.         occ -= tif->tif_scanlinesize;
  143.         row += tif->tif_scanlinesize;
  144.     }
  145.     return (1);
  146. }
  147.  
  148. TIFFInitThunderScan(tif)
  149.     TIFF *tif;
  150. {
  151.     tif->tif_decoderow = ThunderDecodeRow;
  152.     tif->tif_decodestrip = ThunderDecodeRow;
  153.     return (1);
  154. }
  155.